A control table is a table used to direct the control flow of a computer program. Software that uses a control table is said to be table-driven. Programs from decision tables, Humby, E., 2007, Macdonald, 1973 ... Biggerstaff, Ted J. Englewood Cliffs, NJ : Prentice-Hall A control table encodes both the parameters to a conditional expression and a function reference. An interpreter processes a table by evaluating the conditional expression for input data and invoking the selected function. Using a control table can reduce the need for repetitive code that implements the same logic.
In general, the mapping of input parameters can be via any data structure. A common data structure is the lookup table, which provides relatively high performance but at a relatively high memory footprint. An associative array can minimize memory use at the cost of more lookup time.
How the associated behavior is referenced varies. Some languages provide a direct function reference (i.e. pointer) that can be used to invoke a function directly, but some languages do not. Some languages provide for goto to a location (i.e.label). As a fallback, any language allows for mapping input to an index that can then be used to branch to a particular part of the code.
A control table is often used as part of a higher-level algorithm. It can control the main loop of an event-driven program. A relatively advanced use is instructions for a virtual machine similar to bytecode but usually with operations implied by the table structure itself instead of encoded in the table data.
To be feasible, the range of index values should be relatively small. In the example below, the control table is indexed by ASCII value so it has 256 entries (an entry for each ASCII value); omitted entries are shown as '...'. Only the values for the letter A, D, M, and S are important. All other values are uninteresting and set to 0. A two-byte index would require a minimum of 65,536 entries to handle all input possibilities which might consume more memory than is considered worth the value it provides.
| + |
| 0 |
| 0 |
| 1 |
| 0 |
| 4 |
| 0 |
| 3 |
| 0 |
| 2 |
| 0 |
| 0 |
In automata-based programming and pseudoconversational transaction processing, if the number of distinct program states is small, a "dense sequence" control variable can be used to efficiently dictate the entire flow of the main program loop.
Although quite compact – compared to the multiple equivalent If statements – the branch instructions still carry some redundancy, since the branch opcode and condition code mask are repeated alongside the branch offsets. Control tables containing only the offsets to the program labels can be constructed to overcome this redundancy (at least in assembly languages) and yet requiring only minor execution time overhead compared to a conventional branch table.
A control table can act like a switch statement or more generally as a nested if-then-else construct that includes logical predicates (using boolean style AND/OR conditions) for each case. Such a control table provides for a language-independent implementation of what otherwise is a language-dependent construct. The table embodies the essence of a program, stripped of programming language syntax and platform dependent aspects, and condensed to data and implied logic. The meaning of the table includes implied operations instead of being explicit as in a more typical programming paradigm.
Typically, a two-dimensional control table contains value/action pairs and may additionally contain operators and type system information such as the location, size and format of input or output data, whether data conversion is required. The table may contain array index or relative or absolute pointers to generic or customized primitives or functions to be executed depending upon other values in the row.
The type of values used in a control table depends on the computer language used for the interpreter. Assembly language provides the widest scope for data types including machine code for lookup values. Typically, a control table contains values for each possible matching class of input together with a corresponding pointer to an action function. For a language without pointer support, the table can encode an index which can imply an offset value that can be used to accomplish the same as a pointer.
Handler functions often are coded in the same language as the interpreter, but could be in another language provided a suitable inter-language call-linkage mechanism exists. The choice of language for the interpreter and handler functions usually depends on how portable it needs to be across various platforms. There may be several versions of the interpreter to support Porting. A subordinate control table pointer may optionally substitute for a function pointer in the action column if the interpreter supports this construct. This represents a conditional drop to a lower logical level, mimicking structured programming.
The examples below have been chosen partly to illustrate potential performance gains that may not only compensate significantly for the additional tier of abstraction, but also improve upon – what otherwise might have been – less efficient, less maintainable and lengthier code. Although the examples given are for a 'low level' assembly language and for the C language, it can be seen, in both cases, that very few lines of code are required to implement the control table approach and yet can achieve very significant constant time performance improvements, reduce repetitive source coding and aid clarity, as compared with verbose conventional program language constructs.
| + CT1 ! input !! action |
| Add |
| Subtract |
| Multiply |
| Divide |
The next example illustrates how a similar effect can be achieved in a language that does not support pointer definitions in data structures but does support indexed branching to a function – contained within a (0-based) array of function pointers. The table (CT2) is used to extract the index (from 2nd column) to the pointer array (CT2P). If pointer arrays are not supported, a SWITCH statement or equivalent can be used to alter the control flow to one of a sequence of program labels (e.g.: case0, case1, case2, case3, case4) which then either process the input directly, or else perform a call (with return) to the appropriate function.
| + CT2 ! input!! subr # |
| 1 |
| 2 |
| 3 |
| 4 |
| + CT2P ! index | array |
| Add | |
| Subtract | |
| Multiply | |
| Divide |
A two-dimensional control table could be used to support testing multiple conditions or performing more than one action. An action can include a pointer to another subordinate control table. The simple example below has had an implicit 'OR' condition incorporated as an extra column (to handle lower case input, however in this instance, this could equally have been handled by having an extra entry for each of the lower case characters specifying the same function identifier as the upper case characters). An extra column to count the actual run-time events for each input as they occur is also included.
| + CT3 ! input 1!!alternate!! subr # !! count |
| 0 |
| 0 |
| 0 |
| 0 |
The control table entries are then much more similar to conditional statements in procedural languages but, crucially, without the actual (language dependent) conditional statements (i.e. instructions) being present (the generic code is physically in the interpreter that processes the table entries, not in the table itself – which simply embodies the program logic via its structure and values).
In tables such as these, where a series of similar table entries defines the entire logic, a table entry number or pointer may effectively take the place of a program counter in more conventional programs and may be reset in an 'action', also specified in the table entry. The example below (CT4) shows how extending the earlier table, to include a 'next' entry (and/or including an 'alter flow' (jump) function) can create a program loop. (This example is actually not the most efficient way to construct such a control table but, by demonstrating a gradual 'evolution' from the first examples above, shows how additional columns can be used to modify behaviour.) The fifth column demonstrates that more than one action can be initiated with a single table entry – in this case an action to be performed after the normal processing of each entry ('-' values mean 'no conditions' or 'no action').
Structured programming or "Goto-less" code, (incorporating the equivalent of 'do-while' or 'for loop' constructs), can also be accommodated with suitably designed and 'indented' control table structures.
| { class="wikitable" | + CT4 ! input 1!!alternate!! subr # !! count !! jump |
| 1 |
| + CT4P ! index | pointer |
| Default | |
| Add | |
| Subtract | |
| Multiply | |
| Divide | |
| Read Input1 | |
| Alter flow | |
| End |
for (int i = 0; i < sizeof(ops); i++) {
if (op == ops[i]) {
goto *handler_labels[i];
}
}
This would execute faster if a 256 byte table is used to translate the ASCII input () to an index without searching. It would execute in constant time for all values of . If a item contained the name of a function instead of a label, the jump could be replaced with a dynamic function call, eliminating the switch-like but decreasing runtime performance.
size_t i = handler_label_index_by_op(x);
goto *handler_labelsi;
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 1, 0, 0, 4, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0,
0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
};
If the algorithms are not pre-built into the interpreter (and therefore require additional runtime interpretation of an expression held in the table), it is known as "Rule-based Rating" rather than table-driven rating (and consequently consumes significantly more overhead).
Optionally:
|
|